package com.xgkj.diyiketang.activity;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.security.MD5;
import com.xgkj.diyiketang.task.UpdatePasswordTask;
import com.xgkj.diyiketang.util.EatShowSetting;


import android.content.Context;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class UpdatePasswordActivity extends BaseActivity implements OnClickListener {
    private Context mContext;
    private EditText oldPswEdit;
    private EditText newPswEdit;
    private EditText renewPswEdit;
    private String oldPsw;
    private String newPsw;
    private String renewPsw;
    private Button mButton;

    private int initId;
    private int updatePswId;
    private String localPsw;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatepwd);
        mContext = this;
        initView();
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        oldPswEdit = (EditText) findViewById(R.id.updatepsw_yuan);
        oldPswEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        newPswEdit = (EditText) findViewById(R.id.updatepsw_xin);
        newPswEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        renewPswEdit = (EditText) findViewById(R.id.updatepsw_repeat);
        renewPswEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
        mButton = (Button) findViewById(R.id.submit_btn);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (checkInput()) {
            updatePassword();
        }
    }


    private void updatePassword() {
        final UpdatePasswordTask passwordTask = new UpdatePasswordTask(oldPsw, newPsw);
        passwordTask.addListener(new UpdatePasswordTaskEventListener(mContext));
        updatePswId = getEatShowApp().getTaskManager().addTask(passwordTask);

    }

    private boolean checkInput() {
        oldPsw = oldPswEdit.getText().toString();
        newPsw = newPswEdit.getText().toString();
        renewPsw = renewPswEdit.getText().toString();
        if (TextUtils.isEmpty(oldPsw)) {
            Toast.makeText(mContext, R.string.no_old_psw, Toast.LENGTH_SHORT).show();
            oldPswEdit.requestFocus();
            return false;
        } else if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(renewPsw)) {
            Toast.makeText(mContext, R.string.no_new_psw, Toast.LENGTH_SHORT).show();
            newPswEdit.requestFocus();
            return false;
        } else if (!newPsw.equals(renewPsw)) {
            Toast.makeText(mContext, R.string.Two_input_password, Toast.LENGTH_SHORT).show();
            newPswEdit.requestFocus();
            return false;
        }
        oldPsw = MD5.digest(oldPsw);
        newPsw = MD5.digest(newPsw);
        return true;
    }


    class UpdatePasswordTaskEventListener extends ShowToastTaskEventListener {

        public UpdatePasswordTaskEventListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == updatePswId) {

                Toast.makeText(mContext, R.string.success_updatepassword, Toast.LENGTH_SHORT).show();
                EatShowSetting.getInstance(mContext).setLoginPsw(localPsw);
                finish();
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

    }
}